package yl.hs.bmipfull.controllers.finance;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.finance.IReceivePaymentRecordService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * @author 张凯
 * @apiNote 收款记录信息
 * @since 2022/1/12 1:48 下午 创建
 */
@RequestMapping("/api/finance")
@RestController
@Api(value = "付款记录数据接口", tags = "财务管理——付款记录")
public class ReceivePaymentRecordController extends ControllerBase {

    @PutMapping("/receive/payment")
    @ApiOperation(value = "保存收款记录", notes = "保存收款记录" )
    public WebResult savePayment(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel){
        try {
            if (paymentRecordService.savePayment(submitModel)){
             return success("ok");
            } else {
                return fail("保存失败");
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @DeleteMapping("/receive/payment/{sn}")
    @ApiOperation(value = "根据sn删除收款记录", notes = "删除收款记录")
    public WebResult deletePayment(@ApiParam(value = "sn") @PathVariable String sn){
        try {
            if (paymentRecordService.deleteBySn(sn)){
                return success("ok");
            } else {
                return fail("保存失败");
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询收款记录表", notes = "查询收款记录表")
    @RequestMapping(value = "/receive/payment", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery){
        try{
            return success("查询成功",paymentRecordService.queryList(pageQuery));
        }catch(Exception e){
            return error(e);
        }
    }

    @ApiOperation(value = "查询收款记录表", notes = "分页查询收款记录表")
    @RequestMapping(value = "/receive/payment/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum){
        try{
            var result=paymentRecordService.queryPage(query,rowNum,page);
            return success("ok",result);
        }catch(Exception ee){
            return error(ee);
        }
    }

    @ApiOperation(value = "查询收款记录表", notes = "分页查询收款记录表")
    @RequestMapping(value = "money/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryRecordPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum){
        try{
            var result=paymentRecordService.queryRecordPage(query,rowNum,page);
            return success("ok",result);
        }catch(Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "根据合同编号时间查询收款记录表", notes = "根据合同编号时间查询收款记录表")
    @RequestMapping(value = "queryByContractSnAndTime/{contractSn}", method = RequestMethod.GET)
    public WebResult queryByContractSnAndTime(@PathVariable String contractSn){
        try{
            var result=paymentRecordService.queryByContractSnAndTime(contractSn);
            return success("ok",result);
        }catch(Exception ee){
            return fail("无结算数据");
        }
    }
    @Autowired
    public void setPaymentRecordService(IReceivePaymentRecordService paymentRecordService) {
        this.paymentRecordService = paymentRecordService;
    }

    IReceivePaymentRecordService paymentRecordService;

}
